﻿<div class="top-row ps-3 navbar navbar-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="">物联网</a>
        <button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
    <nav class="flex-column">
        @*<div class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                <span class="oi oi-home" aria-hidden="true"></span> Home
            </NavLink>
        </div>
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="counter">
                <span class="oi oi-plus" aria-hidden="true"></span> Counter
            </NavLink>
        </div>
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="fetchdata">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
            </NavLink>
        </div>
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="devicelist">
                <span class="oi oi-list-rich" aria-hidden="true"></span> 设备管理
            </NavLink>
        </div>
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="deviceconfig">
                <span class="oi oi-list-rich" aria-hidden="true"></span> 通信配置
            </NavLink>
        </div>
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="devicepointmanager">
                <span class="oi oi-list-rich" aria-hidden="true"></span> 参数配置
            </NavLink>
        </div>

        <div class="nav-item px-3">
            <NavLink class="nav-link" href="parammonitorlist">
                <span class="oi oi-list-rich" aria-hidden="true"></span> 实时监控
            </NavLink>
        </div>

        <div class="nav-item px-3">
            <NavLink class="nav-link" href="test">
                <span class="oi oi-list-rich" aria-hidden="true"></span> test
            </NavLink>
        </div>*@
        @foreach (var menu in MenuDic)
        {
            if (menu.Key == "")
            {
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="" Match="NavLinkMatch.All" @onclick="async()=>{
await MenuChange(menu.Key);
}">
                        <span class="oi oi-home" aria-hidden="true"></span> Home
                    </NavLink>
                </div>
            }
            else
            {
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="@menu.Key" @onclick="async()=>{
await MenuChange(menu.Key);
}">
                        <span class="oi oi-list-rich" aria-hidden="true"></span> @menu.Value
                    </NavLink>
                </div>
            }
        }
    </nav>
</div>

@code {
    private bool collapseNavMenu = true;

    private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    public void ToggleNavMenu()
    {
        //if (OnValueChanged.HasDelegate)
        //{
        //    await OnValueChanged.InvokeAsync(newValue);
        //}
        Console.WriteLine(collapseNavMenu);
        collapseNavMenu = !collapseNavMenu;
        //NavMenuCssClass => CollapseNavMenu ? "collapse" : null;
    }
    [Parameter]
    public EventCallback<string> OnValueChanged { get; set; }
    private Dictionary<string, string> MenuDic = new Dictionary<string, string> { };
    protected override void OnInitialized()
    {
        MenuDic.Add("", "首页");
        MenuDic.Add("counter", "Counter");
        MenuDic.Add("fetchdata", "Fetch data");
        MenuDic.Add("devicelist", "设备管理");
        MenuDic.Add("deviceconfig", "通信配置");
        MenuDic.Add("devicepointmanager", "参数配置");
        MenuDic.Add("parammonitorlist", "实时监控");
        MenuDic.Add("test", "test");
    }
    private async Task MenuChange(string key)
    {
        Console.WriteLine(key);
        if (OnValueChanged.HasDelegate)
        {
            await OnValueChanged.InvokeAsync($"{key}_{MenuDic[key]}");
        }
    }
}
